package com.charles.mybatis.mapper;

import com.charles.mybatis.Entity.TUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;


public class MybatisQuickStart {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void init() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //1.读取mybatis配置文件sqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        inputStream.close();
    }
    //快速入门
    @Test
    public void quickStart(){
        //2.获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取对应的mapper
        TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
        //4.执行查询语句并返回结果
        TUser tUser = mapper.selectByPrimaryKey(1);
        System.out.println(tUser.toString());

    }
}
